package com.lc.driver;

import com.lc.protocol.IBaseDevice;
import com.lc.protocol.ITransmitHandler;

import org.json.JSONObject;

/**
 * Created by zhyioo on 2017/6/17.
 */

public class JniComTransmitHandler implements IBaseDevice, ITransmitHandler {

    private int _fd = 0;

    protected native int nativeInit(String logDir);

    public JniComTransmitHandler() {
        _fd = nativeInit("");
    }

    public JniComTransmitHandler(String logDir) {
        _fd = nativeInit(logDir);
    }

    public native boolean Open(String dev);

    public native boolean SetBaud(int baud);

    @Override
    public boolean Open(JSONObject sArg) {
        String devName = sArg.optString("Name");
        int baud = sArg.optInt("Baud", 115200);

        boolean bOK = Open(devName);
        if(bOK) {
            bOK = SetBaud(baud);
        }
        return bOK;
    }

    @Override
    public native boolean IsOpen();

    @Override
    public native void Close();

    @Override
    public native boolean OnWrite(byte[] data, int datalen, int timeoutMs);

    @Override
    public native boolean OnRead(byte[] data, int[] datalen, int timeoutMs);

    static {
        try {
            System.loadLibrary("ComDevice");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
